package com.orion.async;

import com.orion.util.SmallTool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Component;

import java.util.concurrent.Future;

/**
 * 异步任务，默认的线程池大小是8，名字叫task-Num，必须加上@EnableAsync注解
 *
 * 调用别的bean的异步方法，这样才生效，不能调用自身的异步方法，否则不生效，这涉及spring的动态代理
 *
 * @author Administrator
 */
@Component
@ConditionalOnProperty(value = "async.asyncTask", havingValue = "true")
public class AsyncTask implements CommandLineRunner {

    @Autowired
    private OtherClazzWithAsyncTaskMethod otherClazzWithAsyncTaskMethod;

    @Override
    public void run(String... strings) throws Exception {
        SmallTool.printTimeAndThread("run async task in other class");
        Future<String> future = otherClazzWithAsyncTaskMethod.customFutureTask();
        SmallTool.printTimeAndThread(
                "let get result form Clazz[OtherClazzWithAsyncTaskMethod]的异步方法customFutureTask() : "
                + future.get());
    }
    /**
     1644742061735	|	1	|	main	|	run async task in other class
     1644742066766	|	1	|	main	|	let get result form Clazz[OtherClazzWithAsyncTaskMethod]的异步方法
                                            customFutureTask() : do Task by thread ：task-1
     */
}
